package com.marketingexpert.controller.friendmgnt;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.marketingexpert.domain.UserType;
import com.marketingexpert.service.snsmgnt.SnsService;
import com.marketingexpert.service.snsmgnt.SnsServiceFactory;
import com.marketingexpert.service.usermgnt.UserService;
import com.marketingexpert.util.WebUtils;

public class GetFriendsController implements Controller {

	private static final int COUNT = 14;
	
	private String sinaSuccess = null;
	private String tencentSuccess = null;
	
	private UserService userService = null;
	
	private SnsServiceFactory snsServiceFactory = null;
	
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		
		
		int cursor = Integer.parseInt(request.getParameter("cursor"));
		String userId = request.getParameter("userId");
		
		int realCursor = cursor * COUNT - COUNT;
		
		com.marketingexpert.domain.User myUser = userService.queryUserByUId(userId);
		
		SnsService weboauth = WebUtils.getSnsService(myUser, getSnsServiceFactory());
		
		
		List<com.marketingexpert.service.snsmgnt.User> friends = weboauth.getFriendsStatuses(myUser.getAccessToken(), myUser.getSecret(), String.valueOf(myUser.getUserId()),realCursor);
		
		int friendsAccounts = weboauth.getUserFriendsCount(myUser.getAccessToken(), myUser.getSecret(), String.valueOf(myUser.getUserId()));
		
		int pageAccounts = -1;
		int beyond = friendsAccounts % COUNT;
		
		if(beyond == 0) {
			pageAccounts = friendsAccounts / COUNT;
		} else {
			pageAccounts = friendsAccounts / COUNT + 1;
		}
		
		request.setAttribute("token",myUser.getAccessToken());
		request.setAttribute("secret", myUser.getSecret());
		
		request.setAttribute("page", cursor);
		request.setAttribute("pageAccounts", pageAccounts);
		request.setAttribute("friends", friends);
		request.setAttribute("userId", userId);
		
		if(myUser.getUserType().equals(UserType.SINAWEIBO)) {
			return new ModelAndView(getSinaSuccess());
		} else {
			return new ModelAndView(getTencentSuccess());
		}
		
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public SnsServiceFactory getSnsServiceFactory() {
		return snsServiceFactory;
	}

	public void setSnsServiceFactory(SnsServiceFactory snsServiceFactory) {
		this.snsServiceFactory = snsServiceFactory;
	}

	public String getSinaSuccess() {
		return sinaSuccess;
	}

	public void setSinaSuccess(String sinaSuccess) {
		this.sinaSuccess = sinaSuccess;
	}

	public String getTencentSuccess() {
		return tencentSuccess;
	}

	public void setTencentSuccess(String tencentSuccess) {
		this.tencentSuccess = tencentSuccess;
	}
	
	

}
